#!/usr/bin/python3
# coding=utf-8

import os
import pytest
from utils.requestsUtil import RequestsUtil
from utils.commonUtil import get_account_info, get_root_path, get_domain
from utils.yamlUtil import YamlUtil


@pytest.fixture(params=[get_domain()])
def mock_func(request):
    def _mock_func(*args, **kwargs):
        demo = None
        user = None
        print(request.param)
        for k,v in kwargs.items():
            if k == "name":
                user = v
        if user:
            demo = {"Content-Type": "application/json", "user": user}
        print(demo)
        return demo

    yield _mock_func


@pytest.fixture(scope="session", autouse=False)
def get_token():
    print("\n >>>>>>>>>>>>>>> 测试用例开始执行 <<<<<<<<<<<<<<")
    reqObj = RequestsUtil()
    user_header = None
    user_info = get_account_info()
    user_name = user_info.get("USER")
    user_pwd = user_info.get("PWD")
    url = get_domain() + "/api/login"
    # method = "post"
    # params = {
    #     "name": user_name,
    #     "password": user_pwd
    # }
    # res = reqObj.send_request(url = url, method = method, params = params )
    if user_pwd:
        user_header = {
            "Content-Type": "application/json; charset=utf-8",
            "Token": str(user_name) + str(user_pwd),
        }
        # 将header写入runtime yaml文件，可能存在重复的header
        runtime_file = os.path.join(get_root_path(), "configs", "runtime_setting.yml")
        YamlUtil(runtime_file).data_truncate()
        YamlUtil(runtime_file).data_append({"admin_header": user_header})
        # print("追加新内容：{}".format(YamlUtil(runtime_file).data_load()))
        yield user_header

    else:
        raise ValueError("用户名或密码不正确, 登入失败。")

    print("\n >>>>>>>>>>>>>>> 测试用例完成执行 <<<<<<<<<<<<<<")
